package com.ruoyi.web.controller.comlbs;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.comlbs.SimCardAccountStatusEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SimCard;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISimCardService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * sim卡管理Controller
 * 
 * @author wp
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/comlbs/simCard")
public class SimCardController extends BaseController
{
    @Autowired
    private ISimCardService simCardService;

    @Autowired
    private ISysConfigService configService;

    @ApiOperation("sim卡管理列表")
    @ApiOperationSupport(includeParameters = {"iccid","deviceId","numberPlate","accountStatus","isOutDate", "isOutDataUsage"})
    @GetMapping("/list")
    public TableDataInfo list(SimCard simCard) {
        simCard.setOutDataUsageValue(Double.valueOf(configService.selectConfigByKey("sim_out_data_usage")));
        startPage();
        List<SimCard> list = simCardService.selectList(simCard);
        return getDataTable(list);
    }

    /**
     * 导出sim卡管理列表
     */
    @ApiOperation("sim卡管理列表导出")
    @ApiOperationSupport(includeParameters = {"iccid","deviceId","numberPlate","accountStatus","expiryStatus"})
    @Log(title = "sim卡管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SimCard simCard)
    {
        List<SimCard> list = simCardService.selectList(simCard);
        ExcelUtil<SimCard> util = new ExcelUtil<SimCard>(SimCard.class);
        util.hideColumn("createTime", "updateTime");
        util.exportExcel(response, list, "sim卡管理数据");
    }

    /**
     * 获取sim卡管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(simCardService.getById(id));
    }

    /**
     * 新增sim卡管理
     */
    @Log(title = "sim卡管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SimCard simCard)
    {
        return toAjax(simCardService.save(simCard));
    }

    /**
     * 修改sim卡管理
     */
    @Log(title = "sim卡管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SimCard simCard)
    {
        return toAjax(simCardService.updateById(simCard));
    }

    /**
     * 删除sim卡管理
     */
    @Log(title = "sim卡管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(simCardService.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation("sim卡到期预警")
    @PostMapping("/earlyWarning")
    public AjaxResult earlyWarning() {
        List<SimCard> simCardList = simCardService.selectList(new SimCard().setIsOutDate(1));
        //提取过期时间与当前时间小于30天的Sim卡(销号的不计算在内)
        List<SimCard> earlyWarningList = simCardList.stream().filter(item -> item.getExpiryDate() != null
                && !SimCardAccountStatusEnum.CANCEL_ACCOUNT.getCode().equals(item.getAccountStatus())
        ).collect(Collectors.toList());
        return success(earlyWarningList);
    }

    @ApiOperation("sim卡超出流量预警")
    @PostMapping("/overflowWarning")
    public AjaxResult overflowWarning() {
        List<SimCard> simCardList = simCardService.selectList(new SimCard());
        //获取流量预警阈值
        double simOutDataUsage = Double.parseDouble(configService.selectConfigByKey("sim_out_data_usage"));
        //提取超出流量阈值的Sim卡
        List<SimCard> overflowWarningList = simCardList.stream().filter(item ->
                StringUtils.isNotEmpty(item.getOutDataUsage()) && Double.parseDouble(item.getOutDataUsage()) > simOutDataUsage
        ).collect(Collectors.toList());
        return success(overflowWarningList);
    }

    @ApiOperation("同步数据")
    @PostMapping("/syncData")
    public AjaxResult syncData() {
        simCardService.simCardUpdateAll();
        return success();
    }

    @ApiOperation("sim超出流量预警值设置")
    @PostMapping("/saveOverflowWarningConfig")
    public AjaxResult saveOverflowWarningConfig(@RequestParam Double value) {
        SysConfig config = configService.getOne(new QueryWrapper<SysConfig>().eq("config_key", "sim_out_data_usage"));
        config.setConfigValue(value.toString());
        return toAjax(configService.updateConfig(config));
    }

}
